#ifndef _CRT_FILE_H_
#define _CRT_FILE_H_

#include <crt_core.hpp>

#ifdef __cplusplus
extern "C" {
#endif

typedef unsigned long file_path_len;
typedef const char* file_path;

#if (TARGET_OS == OS_WIN)

typedef HANDLE _fd_t;
typedef long long _fd_size;
typedef HANDLE _pipe_t;
typedef DWORD _fd_len;
#define FILE_HANDLE        HANDLE
#define FILE_INVALID       INVALID_HANDLE_VALUE;
#define FILE_INVALID_VALUE INVALID_HANDLE_VALUE

#elif (TARGET_OS == OS_POSIX)

typedef int _fd_t;
typedef int _pipe_t;
typedef long _fd_size;
typedef ssize_t _fd_len;
#define FILE_HANDLE        int
#define FILE_INVALID       (int)(-1)
#define FILE_INVALID_VALUE -1

#elif (TARGET_OS == OS_MACH)

typedef int _fd_t;
typedef long long _fd_size;
typedef int _pipe_t;
typedef ssize_t _fd_len;
#define FILE_HANDLE        int
#define FILE_INVALID       (int)(-1)
#define FILE_INVALID_VALUE -1

#elif (TARGET_OS == OS_UNIX)

typedef int _fd_t;
typedef int _pipe_t;
typedef long _fd_size;
typedef ssize_t _fd_len;
#define FILE_HANDLE        int
#define FILE_INVALID       (int)(-1)
#define FILE_INVALID_VALUE -1

#endif

_fd_t _file_snamepipe(const char* name, long rsize, long wsize, int timeout, int flags, int mode);
int _close_snamepipe(_fd_t fd);

_fd_t _file_cnamepipe(const char* name, int flags, int mode);
int _close_cnamepipe(_fd_t fd);

_fd_t _file_open(const basic_tchar* filepath, int flags, int mode);
int _file_close(_fd_t fh);
_fd_size _file_lseek(_fd_t fh, _off_t offset, int whence);
_fd_len _file_read(_fd_t fh, void* buf, size_t size);
_fd_len _file_write(_fd_t fh, const void* buf, size_t size);
int64_t _file_fsize(_fd_t fh);
int _file_delete(const basic_tchar* filepath);
int _file_unlink(const char* filepath);

#ifdef __cplusplus
}
#endif

#endif
